<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryObj.query"
            clearable
            @clear="getOrdersList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              class="search"
              @click="getOrdersList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表区域 -->
      <el-table :data="ordersList" stripe border>
        <el-table-column
          type="index"
          label="编号"
          width="49px"
        ></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column
          prop="order_price"
          label="订单价格(元)"
          width="166px"
        ></el-table-column>
        <el-table-column label="是否付款" width="166px" prop="pay_status">
          <template slot-scope="scoped">
            <el-tag type="danger" v-if="scoped.row.pay_status == '0'"
              >未付款</el-tag
            >
            <el-tag type="success" v-else-if="scoped.row.pay_status == '1'"
              >已付款</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="is_send"
          label="是否发货"
          width="166px"
        ></el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="188px">
          <template slot-scope="scoped">
            {{ scoped.row.create_time | dateForm }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="258px">
          <template slot-scope="scoped">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editAddress"
              >编辑</el-button
            >
            <!-- 删除按钮 -->
            <el-button
              type="success"
              icon="el-icon-location"
              size="mini"
              @click="showStatus(scoped.row)"
              >物流</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryObj.pagenum"
        :page-sizes="[3, 5, 7, 10]"
        :page-size="queryObj.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址弹框 -->
    <el-dialog
      :visible.sync="editAddressDialog"
      title="修改地址"
      width="50%"
      @close="closeEditDialog"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
        status-icon
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="editForm.address1"
            :options="address"
            @change="handleChange"
            style="width:100%"
            filterable
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAddressDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitAddress">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流信息对话框 -->
    <el-dialog
      :visible.sync="showWuLiuStatus"
      title="查看物流进度"
      width="50%"
      @close="closeShowDialog"
    >
    <!-- 时间线 -->
      <el-timeline >
        <el-timeline-item
          v-for="(progress, index) in progressInfo"
          :key="index"
          :timestamp="progress.time"
        >
          {{ progress.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import address from './citydata.js'
export default {
  data() {
    return {
      //列表请求参数对象
      queryObj: {
        //搜索关键字
        query: '',
        //当前页面值
        pagenum: 1,
        //显示数据的条数
        pagesize: 10
      },
      //列表数据对象
      ordersList: [],
      //数据总条数
      total: 0,
      //控制修改地址弹框显示与隐藏
      editAddressDialog: false,
      //修改地址表单参数
      editForm: {
        address1: [],
        address2: ''
      },
      //修改表单校验规则
      editFormRules: {
        address1: [
          { required: true, message: '请选择省市区', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      //省级联动数据
      address,
      //控制物流对话框的显示隐藏
      showWuLiuStatus: false,
      //物流信息数组
      progressInfo: []
    }
  },
  created() {
    //获取订单列表
    this.getOrdersList()
  },
  methods: {
    //获取订单列表
    async getOrdersList() {
      const res = await this.$http.get('orders', { params: this.queryObj })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      //获取成，把返回的数据赋给ordersList
      this.ordersList = res.data.goods
      this.total = res.data.total
    },
    //监听pagesize改变事件
    handleSizeChange(newSize) {
      // console.log(newSize);
      this.queryObj.pagesize = newSize
      this.getOrdersList()
    },
    //监听 页码值改变事件
    handleCurrentChange(newPage) {
      // console.log(newPage);
      this.queryObj.pagenum = newPage
      this.getOrdersList()
    },
    //点击编辑按钮显示修改地址弹框
    editAddress() {
      //显示弹框
      this.editAddressDialog = true
    },
    //点击确定提交修改地址表单
    submitAddress() {
      this.editAddressDialog = false
    },
    //监听编辑弹框关闭事件
    closeEditDialog() {
      this.$refs.editFormRef.resetFields()
      this.editForm.address1 = []
    },
    //监听物流弹框关闭事件
    closeShowDialog() {},
    //查看物流信息
    async showStatus() {
      this.showWuLiuStatus = true
      const res = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)

      //获取成功
      this.progressInfo = res.data
      console.log(this.progressInfo)
    },
    //监听级联选择器的变化
    handleChange() {}
  }
}
</script>

<style lang="less" scoped></style>
